<template>
  <tl-sub-page :render="render" :visible.sync="dialogVisible" v-dialogDrag :title="title" :close-on-click-modal="false" class="tl-dialog" :fullscreen="true" append-to-body @close="close">
    <el-collapse v-model="activeNames">
      <el-collapse-item name="1">
        <template slot="title">
          <div class="tl-form-line-title sub-title" style="height: auto;width: 100%;line-height: 28px;">
            <span style="float:left">{{title}}</span>
            <el-row class="score-title">
              <el-col>
                <el-input size="mini" style="color: black;" @change="changescore" v-model="totalfullscore" :disabled="!editdisabled && !fromReview" oninput="this.value=this.value.match(/\d{0,}\.?\d{0,1}/)" >
                  <template slot="prepend">满分合计</template>
                  <template slot="append">分</template>
                </el-input>
              </el-col>
            </el-row>
            <span class="text-hover" style="fontSize: 16px" @click.stop="showFullScreen" v-show="render==='div'">&nbsp;&nbsp;全屏展示</span>
            <!-- <span class="text-hover" style="fontSize: 16px;color:#409EFF" @click.stop="doExportExcel">&nbsp;&nbsp;导出</span> -->
            <div class="statistic-title">
              <span style="margin-right: 20px">完成得分合计：{{completionscoretotal}}</span>
              <span v-if="!(parseFloat(selfscoretotal) > 100 || (parseFloat(selfscoretotal) < 90 && times === '2'))" style="margin-right: 20px" >自评分合计：{{selfscoretotal}}</span>
              <span  v-if="parseFloat(selfscoretotal) > 100 || (parseFloat(selfscoretotal) < 90 && times === '2')" style="margin-right: 20px;color:red" >自评分合计：{{selfscoretotal}}</span>
              <span v-if="parseFloat(targetscoretotal) <= 100" style="margin-right: 20px">目标分合计：{{targetscoretotal}}</span>
              <span v-if="parseFloat(targetscoretotal) > 100" style="margin-right: 20px;color:red">目标分合计：{{targetscoretotal}}</span>
              <span>本周提升分数合计：{{improvetotalscore}}</span>
            </div>
          </div>
        </template>
        <!--表格渲染-->
        <el-table class="tl-table" ref="mainTable" size="small" :resizable-refs="tableResizeRefs" v-loading="loading" :data="datas" @selection-change="doSelectChange" @sort-change="doSortChange" @row-dblclick="doRowDbClick" @row-click="doRowClick" row-key="id" border fit :row-class-name="doChangeRowClassName">
          <el-table-column type="index" label="序号" width="55px" align="center"></el-table-column>
          <el-table-column prop="scoringitem_big_catergory" label="评分项目（大类）" width="150px" sortable="custom" sort-by="scoringitem_big_catergory" align="left" header-align="center" :show-overflow-tooltip="true"></el-table-column>
          <el-table-column prop="scoringitem_small_catergory" label="评分项目（小类）" width="150px" sortable="custom" sort-by="scoringitem_small_catergory" align="left" header-align="center" :show-overflow-tooltip="true" ></el-table-column>
          <el-table-column prop="scoringrubric" label="评分细则"   align="left" header-align="center" :show-overflow-tooltip="true"></el-table-column>
          <el-table-column prop="scoredescription" label="细则描述" width="150px"  align="left" header-align="center" :show-overflow-tooltip="true"></el-table-column>
          <el-table-column prop="fullscore" label="满分" width="50px" sortable="custom" sort-by="fullscore" align="center" header-align="center" :show-overflow-tooltip="true"></el-table-column>
          <el-table-column prop="selfscore" label="自评分" width="70px" sortable="custom" sort-by="selfscore" align="center" header-align="center" :show-overflow-tooltip="true"></el-table-column>
          <el-table-column prop="targetscore" label="目标得分" width="85px" sortable="custom" sort-by="targetscore" align="center" header-align="center" :show-overflow-tooltip="true"></el-table-column>
          <el-table-column prop="scoregap" label="分差" width="50px" sortable="custom" sort-by="scoregap" align="center" header-align="center" :show-overflow-tooltip="true"></el-table-column>
          <el-table-column v-if="!fromBidApproval" prop="dispisexpertguide" label="专家指导" width="100px" sortable="custom" sort-by="isexpertguide" align="center" header-align="center" :show-overflow-tooltip="true"></el-table-column>
          <el-table-column v-if="!fromBidApproval" prop="gapanalysis" label="基于专家意见的差距分析" width="200px" sortable="custom" sort-by="gapanalysis" align="left" header-align="center" :show-overflow-tooltip="true"></el-table-column>
          <el-table-column v-if="!fromBidApproval" prop="scoringplan" label="提分计划" sortable="custom" sort-by="scoringplan" align="left" header-align="center" :show-overflow-tooltip="true"></el-table-column>
          <el-table-column prop="responsible" label="责任人" width="70px" sortable="custom" sort-by="responsible" align="center" header-align="center" :show-overflow-tooltip="true"></el-table-column>
          <el-table-column prop="planenddate" label="计划完成日期" width="110px" sortable="custom" sort-by="planenddate" align="center" header-align="center" :formatter="dateFormat" :show-overflow-tooltip="true"></el-table-column>
          <el-table-column v-if="!fromBidApproval" prop="improvescore" label="本周提升分数" width="110px" sortable="custom" sort-by="improvescore" align="center" header-align="center" :show-overflow-tooltip="true"></el-table-column>
          <el-table-column v-if="!isapproval && !fromBidApproval" prop="completion" label="进度更新" width="150px" sortable="custom" sort-by="completion" align="left" header-align="center" :show-overflow-tooltip="true">
            <template slot-scope="scope">
              <span v-if="scope.row.completion">{{scope.row.completion}}</span>
            </template>
          </el-table-column>
          <el-table-column v-if="!isapproval" prop="completionscore" label="完成得分" sortable="custom" sort-by="completionscore" align="center" header-align="center" :show-overflow-tooltip="true">
            <template slot-scope="scope">
              <span v-if="scope.row.completionscore">{{scope.row.completionscore}}</span>
              <span v-else ></span>
            </template>
          </el-table-column>
          <el-table-column v-if="!isapproval && (isshowlossscorepoint || isshowlossadvice)" prop="feedbackscore"  width="150px" label="专家反馈得分" sortable="custom" sort-by="feedbackscore" align="center" header-align="center" :show-overflow-tooltip="true"></el-table-column>
          <el-table-column v-if="!isapproval && isshowlossscorepoint" prop="lossscorepoint" label="失分点" width="150px" sortable="custom" sort-by="lossscorepoint" align="left" header-align="center" :show-overflow-tooltip="true"></el-table-column>
          <el-table-column v-if="!isapproval && isshowlossscorepoint" prop="improvementsuggestion" label="改进意见" width="150px" sortable="custom" sort-by="improvementsuggestion" align="center" header-align="center" :show-overflow-tooltip="true"></el-table-column>
          <el-table-column v-if="!isapproval && isshowlossadvice" prop="losspointandadvice" label="失分点及改进意见" width="200px" sortable="custom" sort-by="losspointandadvice" align="left" header-align="center" :show-overflow-tooltip="true"></el-table-column>
          <el-table-column prop="dispstate" label="审核状态" width="80px" sortable="custom" sort-by="state" align="center" header-align="center" :show-overflow-tooltip="true"></el-table-column>
          <el-table-column label="操作" width="100px" v-slot="scope" fixed="right">
            <el-dropdown trigger="hover">
              <el-button type="text" icon="el-icon-s-operation" style="margin-left:10px" @click.stop>更多<i class="el-icon-arrow-down el-icon--right"></i></el-button>
              <el-dropdown-menu>
                <el-dropdown-item icon="el-icon-view" @click.native="openViewDialog('editForm', scope.row.id)">查看</el-dropdown-item>
                <el-dropdown-item icon="el-icon-edit" v-if="(editdisabled && (scope.row.state === 0 || scope.row.state === 2)) || fromReview" @click.native="openEditDialog('editForm', scope.row.id)">编辑</el-dropdown-item>
                <el-dropdown-item icon="el-icon-delete" v-if="editdisabled && (scope.row.state === 2 || scope.row.state === 0)" @click.native="doDelete(scope.row.id)">删除</el-dropdown-item>
                <el-dropdown-item icon="el-icon-edit" v-if="(scope.row.responsiblecode === user.code || isPM) && !scope.row.actualenddate && scope.row.state === 9 && !fromReview" @click.native="openScoreDialog('feedbackForm', scope.row)">自评反馈</el-dropdown-item>
                <!-- <el-dropdown-item icon="el-icon-edit" v-if="scope.row.usercode === user.code && scope.row.state === 9 && !fromReview" @click.native="openScoreDialog('improveForm', scope.row)">本周提升分数</el-dropdown-item> -->
                <el-dropdown-item icon="el-icon-edit" v-if="isPM && scope.row.state === 9 && !fromReview" @click.native="openChangeDialog('editForm', scope.row.id)">变更</el-dropdown-item>
                <el-dropdown-item icon="el-icon-view" v-if="scope.row.state === 9" @click.native="openHistoryDialog(scope.row.id)">历史</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </el-table-column>
        </el-table>
        <tl-edit ref="editForm" @queryIsRefresh="queryIsRefresh" :totalfullscore="totalfullscore" :feedbackList="feedbackList" :biddate="biddate" :projectinfo="projectinfo" @refresh="doRefresh" :fromReview="fromReview"></tl-edit>
        <tl-feed-back-score ref="feedbackForm" :biddate="biddate" :feedbackList="feedbackList" :projectinfo="projectinfo" @refresh="doRefresh"></tl-feed-back-score>
        <tl-improve-score ref="improveForm" :biddate="biddate" :projectinfo="projectinfo" @refresh="doRefresh"></tl-improve-score>
        <tl-his-component ref="scorehistoryref" :times="times" :type="type" :projectinfo="projectinfo" :title="title"></tl-his-component>
        <center><button v-if="editdisabled && (parseFloat(selfscoretotal) <= 100 && parseFloat(targetscoretotal) <= 100) && parseFloat(totalfullscore) > 0" type="text" size="medium" icon="el-icon-document-add" @click.prevent="openAddScore('editForm', times, type)">+</button></center>
      </el-collapse-item>
    </el-collapse>
  </tl-sub-page>
</template>

<script>
  /** mixins */
  import { mixin_list } from '@tapui/mixins/mixin_list'
  /** 子组件 */
  import tlEdit from './edit'
  import tlFeedBackScore from './feedBackScore'
  import tlChangeEdit from './changeEdit'
  import tlHisComponent from './historyComponent'
  import tlImproveScore from './improveScore'
  /** 工具类 */
  import { getAction, putAction, postAction, deleteAction } from '@tapui/utils/http'
  import { checkPermission } from '@tapui/utils/permission'
  export default {
    name: 'mpmProjectBidScore',
    mixins: [mixin_list],
    components: { tlEdit, tlFeedBackScore, tlChangeEdit, tlHisComponent, tlImproveScore },
    data() {
      return {
        description: '评标细则信息表-列表查询',
        urlPrefix: '/mpm/projectBidScore/',
        url: {
          queryCodeList: '/mpm/projectBidScore/dataCodeList',
          fullScoreInfo: '/mpm/projectBidFullScore/fullScoreInfo',
          queryFeedBackIsApproval: '/mpm/projectBidScore/queryFeedBackIsApproval',
          queryIsRefresh: '/mpm/projectBidScore/queryIsRefresh'
        },
        // 排序参数
        sorterInfo: {
          sidx: 'sortcode',
          sord: 'asc'
        },
        selfscoretotal: 0,
        targetscoretotal: 0,
        completionscoretotal: 0,
        fullscoretotal: 0,
        iscanpublish: true,
        totalfullscore: '100',
        passTotalfullscore: 0,
        improvetotalscore: 0,
        feedbackList: [],
        isshowlossscorepoint: false,
        activeNames: ['1'],
        pageInfo: {
          currentPage: 1, // 当前页面
          pageSize: 100, // 每页记录数
          pageSizes: ['10', '50', '100'],
          totalCount: 0 // 总数
        },
        dialogVisible: false,
        isshowlossadvice: false,
        clos: "scoringitem_big_catergory;评分项目大类,scoringitem_small_catergory;评分项目小类,scoringrubric;评分细则,scoredescription;细则描述," +
        "fullscore;满分,selfscore;自评分,targetscore;目标得分,scoregap;分差,gapanalysis;基于专家意见的差距分析,scoringplan;提分改进计划,responsible;责任人," +
        "planenddate;计划完成日期,completionscore;完成得分,expertquestionsList;专家问题清单"
      }
    },
    props: {
      projectinfo: {},
      title: {
        type: [String],
        default: ''
      },
      times: {
        type: [String],
        default: '1'
      },
      type: {
        type: [String],
        default: '1'
      },
      editdisabled: {
        type: Boolean,
        default: false
      },
      isapproval: {
        type: Boolean,
        default: false
      },
      biddate: {
        type: Date,
        default: () => new Date()
      },
      // 是否来自复盘页面
      fromReview: {
        type: Boolean,
        default: false
      },
      // 是否可编辑评分细则反馈
      editFeedback: {
        type: Boolean,
        default: true
      },
      render: {
        default: 'div',
        type: String
      },
      // 是否来自反馈的投标部审批页面
      fromBidApproval: {
        type: Boolean,
        default: false
      }
    },
    computed: {
      // 按钮的控制
      showEditButton() {
        if (this.projectinfo.projectmanagercode !== this.user.code) {
          return false
        }
        return true
        // return (row) => {
        //   if (!this.isshowExport || row.state !== 1) {
        //     return false
        //   }
        //   return true
        // }
      },
      isPM() {
        if (this.$store.getters.projectroles) {
          return this.$store.getters.projectroles.indexOf('pm') > -1
        } else {
          return false
        }
      }
    },
    methods: {
      showFullScreen() {
        this.$emit('showFullScreen')
      },
      close() {
        this.$emit('refresh')
      },
      queryIsRefresh(sourceid) {
        return getAction(this.url.queryIsRefresh, { id: sourceid }).then((res) => {
          if (res.success) {
            if (res.result.key) {
              if (res.result.type === '1') {
                this.$parent.$refs.line3.loadData()
              } else if (res.result.type === '2') {
                this.$parent.$refs.line4.loadData()
              }
            }
          } else {
            this.$tap.alert(res.message, 'warning')
          }
        })
      },
      fullScoreInfo() {
        if (!this.projectinfo) {
          return false
        }
        return getAction(this.url.fullScoreInfo, { times: this.times, type: this.type, projectcode: this.projectinfo.projectcode }).then((res) => {
          if (res.success) {
            if (!res.result.totalfullsocre || res.result.totalfullsocre === '') {
              this.totalfullscore = '100'
            } else {
              this.totalfullscore = res.result.totalfullsocre
            }
            this.$emit("getFullScoreInfo", res.result)
          } else {
            this.$tap.alert(res.message, 'warning')
          }
        })
      },
      changescore() {
        // 满分
        let totalfullscore = parseFloat(this.totalfullscore ? this.totalfullscore : '0')
        if (totalfullscore > 100) {
          this.$tap.alert('满分不能大于100！')
          this.totalfullscore = '100'
          this.$emit('getTotalScore', this.totalfullscore, this.type)
          return
        }
        if (totalfullscore < this.fullscoretotal) {
          this.$tap.alert(' 总分合计不能小于已填写的总分数！')
          if (parseFloat(this.fullscoretotal) > 100) {
            this.totalfullscore = '100'
          } else {
            this.totalfullscore = this.fullscoretotal + ''
          }
          this.$emit('getTotalScore', this.totalfullscore, this.type)
          return
        }
        this.$emit('getTotalScore', totalfullscore, this.type)
      },
      // 打开线索历史表
      openHistoryDialog(id) {
        this.$refs.scorehistoryref.visible = true
        this.$refs.scorehistoryref.sourceid = id
      },
      /** 变更 */
      openChangeDialog(editForm, id) {
        this.$refs[editForm].id = id
        this.$refs[editForm].isAdd = false
        this.$refs[editForm].visible = true
        this.$refs[editForm].disableEditSubmit = false
        this.$refs[editForm].isChange = true
        let that = this
        setTimeout(() => {
          that.clickRowkey = id
        }, 100)
      },
      openScoreDialog(editFrom, row) {
        getAction(this.url.queryFeedBackIsApproval, { id: row.id }).then((res) => {
          if (res.success) {
            let key = res.result.key
            this.feedbackList = res.result.feedbackList
            if (key) {
              this.$refs[editFrom].isAdd = true
            } else {
              this.$refs[editFrom].isAdd = false
              this.$refs[editFrom].id = res.result.feedbackid
            }
            this.$refs[editFrom].visible = true
            this.$refs[editFrom].bidScoreInfo = row
          } else {
            this.$tap.alert(res.message, 'warning')
          }
        })
      },
      openAddScore(editFrom, times, type) {
        this.$emit('doSave')
        this.$refs[editFrom].visible = true
        this.$refs[editFrom].isAdd = true
        this.$refs[editFrom].disableEditSubmit = false
        this.$refs[editFrom].type = type
        this.$refs[editFrom].times = times
      },
      // 禁用表格前面的选择
      doDisableCheckBox(row, index) {
        if (row.state !== 0 && row.state !== 2) {
          return false
        } else {
          return true
        }
      },
      beforeLoadData() {
        this.fullScoreInfo()
        if (!this.projectinfo || !this.projectinfo.projectcode) {
          return false
        }
        // this.$emit('queryScoreInfo')
        this.queryParam.qry_projectcode = this.projectinfo.projectcode
        this.queryParam.qry_type = this.type
        this.queryParam.qry_times = this.times
        if (!this.fromReview) {
          this.queryParam.isapproval = this.isapproval
        }
        return true
      },
      afterLoadData() {
        this.selfscoretotal = 0
        this.targetscoretotal = 0
        this.fullscoretotal = 0
        this.passTotalfullscore = 0
        this.completionscoretotal = 0
        this.improvetotalscore = 0
        for (let i = 0; i < this.datas.length; i++) {
          this.selfscoretotal = parseFloat(this.selfscoretotal) + parseFloat(this.datas[i].selfscore)
          this.targetscoretotal = parseFloat(this.targetscoretotal) + parseFloat(this.datas[i].targetscore)
          // 填写的表格里的总分合计
          this.fullscoretotal = parseFloat(this.fullscoretotal) + parseFloat(this.datas[i].fullscore)
          // 完成得分合计
          this.completionscoretotal = parseFloat(this.completionscoretotal) + (parseFloat(this.datas[i].completionscore) > 0 ? parseFloat(this.datas[i].completionscore) : 0)
          // 通过的总分
          if (this.datas[i].state === 9) {
            this.passTotalfullscore = parseFloat(this.passTotalfullscore) + parseFloat(this.datas[i].fullscore)
          }
          let a = parseFloat(this.datas[i].improvescore)
          this.improvetotalscore = parseFloat(this.improvetotalscore) + (parseFloat(this.datas[i].improvescore) > 0 ? parseFloat(this.datas[i].improvescore) : 0)
          if (this.datas[i].lossscorepoint || this.datas[i].improvementsuggestion) {
            this.isshowlossscorepoint = true
          } else if (this.datas[i].losspointandadvice) {
            this.isshowlossadvice = true
          }
        }
        this.selfscoretotal = ('' + this.selfscoretotal).indexOf('.') > 0 ? Number(this.selfscoretotal).toFixed(1) : this.selfscoretotal
        this.targetscoretotal = ('' + this.targetscoretotal).indexOf('.') > 0 ? Number(this.targetscoretotal).toFixed(1) : this.targetscoretotal
        this.fullscoretotal = ('' + this.fullscoretotal).indexOf('.') > 0 ? Number(this.fullscoretotal).toFixed(1) : this.fullscoretotal
        this.completionscoretotal = ('' + this.completionscoretotal).indexOf('.') > 0 ? Number(this.completionscoretotal).toFixed(1) : this.completionscoretotal
        this.passTotalfullscore = ('' + this.passTotalfullscore).indexOf('.') > 0 ? Number(this.passTotalfullscore).toFixed(1) : this.passTotalfullscore
        this.improvetotalscore = ('' + this.improvetotalscore).indexOf('.') > 0 ? Number(this.improvetotalscore).toFixed(1) : this.improvetotalscore
        // 分数填写满足要求且有草稿状态的
        this.iscanpublish = !(parseFloat(this.selfscoretotal) > 100 || parseFloat(this.targetscoretotal) > 100)
        this.$emit('isloading')
        this.$emit('haveBidScore')
        this.getIfCheck()
        this.$refs.mainTable.doLayout()
      },
      doRowDbClick(row, column, event) {
        // 解决双击调用点击事件防范问题
        clearTimeout(this.clickTimer)
        if ((this.editdisabled && row.state !== 9 && row.state !== 1) || this.fromReview) {
          this.rowDbClickActionType = 'edit'
        } else {
          this.rowDbClickActionType = 'view'
        }
        if (this.rowDbClickActionType === 'edit') {
          // 判断是否有权限
          if (this.editPermission.length > 0) {
            if (checkPermission(this.editPermission)) {
              this.openEditDialog('editForm', row[this.$refs.mainTable.rowKey])
            } else {
              this.openViewDialog('editForm', row[this.$refs.mainTable.rowKey])
            }
          } else {
            this.openEditDialog('editForm', row[this.$refs.mainTable.rowKey])
          }
        } else if (this.rowDbClickActionType === 'view') {
          this.openViewDialog('editForm', row[this.$refs.mainTable.rowKey])
        }
      },
      exportExportExcel() {
      //   let cols = []
      //   this.$refs.mainTable.$children.forEach((col) => {
      //   if (col.$options._componentTag === 'el-table-column') {
      //     if (col.$options.propsData.prop) {
      //       cols.push(col.$options.propsData.prop + ';' + col.$options.propsData.label)
      //     }
      //   }
      // })
      let data = {}
      Object.assign(data, this.getQueryParams(), { cols: this.clos })
      // data.cols = 'disptype;类型,' + data.cols
      this.$emit('getdata', data)
      },
      // 检查是否审核
      getIfCheck() {
        var res = false
        for (let i = 0; i < this.datas.length; i++) {
          if (this.datas[i].state === 1 || this.datas[i].state === 9) {
              res = true
              break
          }
        }
        this.$emit("getIfCheck", res)
      },
       // 检查是否有数据
      getIfHasData() {
        if (this.datas.length > 0) {
           this.$emit("HasData", true)
        } else {
           this.$emit("HasData", false)
        }
      },
       // 检查满分合计是否有数据
      getIfHasTotalfullscore() {
        if (this.totalfullscore === null || this.totalfullscore === '' || this.totalfullscore === '0' || this.totalfullscore === undefined) {
           this.$emit("HasTotalfullscore", false)
        } else {
           this.$emit("HasTotalfullscore", true)
        }
      }

    }
  }
</script>

<style lang="scss" scoped>
  $small:600px;
  $medium:1380px;

  .statistic-title {
    float:right;
    @media screen and (max-width:$medium) {
      font-size: 16px;
    }
  }
  .score-title {
    float:left;
    margin-left: 10px;
    width: 223px;
  }
  .text-hover:hover {
    color: #0A6ED2;
    cursor: pointer;
    font-weight: bold;
  }
</style>

